home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP13.ZIP / PATRON / PAGEMOUS.CPP < prev    next >
C/C++ Source or Header  |  1993-06-27  |  19KB  |  761 lines

  1. /*
  2.  * PAGEMOUS.CPP
  3.  * Modifications for Chapter 13: None
  4.  *
  5.  * Implementation of mouse-related member functions of CPage.
  6.  * The remainder is in PAGE.CPP.  This separate file keeps this
  7.  * grungy hit-testing/drawing code out of our way.
  8.  *
  9.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Software Design Engineer
  12.  * Microsoft Systems Developer Relations
  13.  *
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19. #include "patron.h"
  20.  
  21.  
  22. //Lookups into the array using g_rgHTCode[x+y*3] in PAGEMOUS.CPP
  23. #define YTOP            0
  24. #define YMID            1
  25. #define YBOT            2
  26. #define XLEFT           0
  27. #define XMID            1
  28. #define XRIGHT          2
  29.  
  30. //Values to restrict sizing in CPage::OnMouseMove
  31. #define SIZINGTOP       0x0001
  32. #define SIZINGBOTTOM    0x0002
  33. #define SIZINGLEFT      0x0004
  34. #define SIZINGRIGHT     0x0008
  35.  
  36.  
  37. //This array is for hit-testing lookups
  38. static UINT g_rgHTCode[9]={HTTOPLEFT, HTTOP, HTTOPRIGHT
  39.     , HTLEFT, HTCLIENT, HTRIGHT, HTBOTTOMLEFT, HTBOTTOM, HTBOTTOMRIGHT};
  40.  
  41.  
  42. //This is for restricting tracking based on the hit-test
  43. static UINT g_rguSizingFlags[9]={SIZINGTOP | SIZINGLEFT, SIZINGTOP
  44.     , SIZINGTOP | SIZINGRIGHT, SIZINGLEFT, 0, SIZINGRIGHT
  45.     , SIZINGBOTTOM | SIZINGLEFT, SIZINGBOTTOM, SIZINGBOTTOM | SIZINGRIGHT};
  46.  
  47.  
  48. /*
  49.  * CPage::OnRightDown
  50.  *
  51.  * Purpose:
  52.  *  Called when the user clicks with the right button on this page.
  53.  *  If there is an object here, determined by the last hit-test code,
  54.  *  the we'll make a popup-menu for it.
  55.  *
  56.  * Parameters:
  57.  *  uKeys           UINT carrying the key state.
  58.  *  x, y            UINT coordinates of the click in device units.
  59.  *
  60.  * Return Value:
  61.  *  BOOL            Indicates if the action changed the object.
  62.  */
  63.  
  64. BOOL CPage::OnRightDown(UINT uKeys, UINT x, UINT y)
  65.     {
  66.     HMENU       hMenu;
  67.     HMENU       hMenuRes;
  68.     HINSTANCE   hInst;
  69.     HWND        hWndFrame, hWndT;
  70.     POINT       pt;
  71.     UINT        i, cItems;
  72.  
  73.     //Select the tenant under the mouse, if there is one.
  74.     if (!FSelectTenantAtPoint(x, y))
  75.         return FALSE;
  76.  
  77.     /*
  78.      * Get the top-level window to which menu command will go.  This will
  79.      * be whatever parent doesn't have a parent itself...
  80.      */
  81.     hWndT=GetParent(m_hWnd);
  82.  
  83.     while (NULL!=hWndT)
  84.         {
  85.         hWndFrame=hWndT;
  86.         hWndT=GetParent(hWndT);
  87.         }
  88.  
  89.     /*
  90.      * Build a popup menu for this object with Cut, Copy, Delete, and
  91.      * object verbs.
  92.      */
  93.     hInst=GETWINDOWINSTANCE(m_hWnd);    //Macro in WIN1632.H
  94.     hMenuRes=LoadMenu(hInst, MAKEINTRESOURCE(IDR_RIGHTPOPUPMENU));
  95.  
  96.     if (NULL==hMenuRes)
  97.         return FALSE;
  98.  
  99.     //Resource-loaded menus don't work, so we'll copy the items.
  100.     hMenu=CreatePopupMenu();
  101.     cItems=GetMenuItemCount(hMenuRes);
  102.  
  103.     for (i=0; i < cItems; i++)
  104.         {
  105.         char        szTemp[80];
  106.         int         id, uFlags;
  107.  
  108.         GetMenuString(hMenuRes, i, szTemp, sizeof(szTemp), MF_BYPOSITION);
  109.         id=GetMenuItemID(hMenuRes, i);
  110.  
  111.         uFlags=(0==id) ? MF_SEPARATOR : MF_STRING | MF_ENABLED;
  112.         AppendMenu(hMenu, uFlags, id, szTemp);
  113.         }
  114.  
  115.     DestroyMenu(hMenuRes);
  116.  
  117.     //Munge the Object menu item
  118.     m_pTenantCur->AddVerbMenu(hMenu, MENUPOS_OBJECTONPOPUP);
  119.  
  120.     //Enable or disable the Links item.
  121.     i=FQueryLinksInPage() ? MF_ENABLED : MF_DISABLED | MF_GRAYED;
  122.     EnableMenuItem(hMenu, IDM_EDITLINKS, i | MF_BYCOMMAND);
  123.  
  124.     SETPOINT(pt, x, y);
  125.     ClientToScreen(m_hWnd, &pt);
  126.  
  127.     TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON
  128.         , pt.x, pt.y, 0, hWndFrame, NULL);
  129.  
  130.     DestroyMenu(hMenu);
  131.     return FALSE;
  132.     }
  133.  
  134.  
  135.  
  136.  
  137. /*
  138.  * CPage::FSelectTenantAtPoint
  139.  *
  140.  * Purpose:
  141.  *  Selects whatever tenant is at the point (x,y) if there is one,
  142.  *  deselecting the previously selected tenant.
  143.  *
  144.  * Parameters:
  145.  *  x, y            UINT coordinates of the mouse.
  146.  *
  147.  * Return Value:
  148.  *  BOOL            TRUE if there is a tenant here, FALSE otherwise.
  149.  */
  150.  
  151. BOOL CPage::FSelectTenantAtPoint(UINT x, UINT y)
  152.     {
  153.     UINT            iTenant;
  154.     LPTENANT        pTenant;
  155.  
  156.     iTenant=TenantFromPoint(x, y, &pTenant);
  157.  
  158.     if (NULL==pTenant)
  159.         return FALSE;
  160.  
  161.     //If this one is already current, we might be now sizing.
  162.     if (pTenant==m_pTenantCur)
  163.         return TRUE;
  164.  
  165.     //Deselect the current tenant
  166.     if (NULL!=m_pTenantCur)
  167.         m_pTenantCur->Select(FALSE);
  168.  
  169.     //Move this tenant to the top of the list
  170.     m_iTenantCur=0;
  171.  
  172.     SendMessage(m_hWndTenantList, LB_DELETESTRING, iTenant, 0L);
  173.     SendMessage(m_hWndTenantList, LB_INSERTSTRING, 0, (LONG)pTenant);
  174.  
  175.     //Select and repaint the new tenant to show it up front
  176.     m_pTenantCur=pTenant;
  177.  
  178.     m_pTenantCur->Repaint();
  179.     m_pTenantCur->Select(TRUE);
  180.  
  181.     return TRUE;
  182.     }
  183.  
  184.  
  185.  
  186.  
  187. /*
  188.  * CPage::OnLeftDown
  189.  *
  190.  * Purpose:
  191.  *  Called when the user clicks with the left button on this page.
  192.  *  We find the object under that position that is visibly on top
  193.  *  (always the first one under this location in the page list since
  194.  *  we paint in reverse order) and select it.
  195.  *
  196.  * Parameters:
  197.  *  uKeys           UINT carrying the key state.
  198.  *  x, y            UINT coordinates of the click in device units.
  199.  *
  200.  * Return Value:
  201.  *  BOOL            Indicates if the action changed the object.
  202.  */
  203.  
  204. BOOL CPage::OnLeftDown(UINT uKeys, UINT x, UINT y)
  205.     {
  206.     RECT        rc;
  207.  
  208.     if (HTCAPTION==m_uHTCode)
  209.         return DragDrop(uKeys, x, y);
  210.  
  211.     /*
  212.      * See if we have to start sizing (which always happens on current
  213.      * tenant).  m_uHTCode is set in OnNCHitTest below.
  214.      */
  215.     if (HTNOWHERE!=m_uHTCode && HTCLIENT!=m_uHTCode)
  216.         {
  217.         //We are sizing, so start tracking
  218.         m_pTenantCur->RectGet(&m_rcl, TRUE);
  219.         SetCapture(m_hWnd);
  220.         m_fTracking=TRUE;
  221.  
  222.         m_hDC=GetDC(m_hWnd);
  223.  
  224.         //Place the rectangle exactly where it is on the screen.
  225.         RECTFROMRECTL(rc, m_rcl)
  226.         OffsetRect(&rc, -(int)m_pPG->m_xPos, -(int)m_pPG->m_yPos);
  227.         RECTLFROMRECT(m_rcl, rc);
  228.         m_rclOrg=m_rcl;
  229.  
  230.         DrawFocusRect(m_hDC, &rc);
  231.  
  232.         m_pPG->CalcBoundingRect(&rc, TRUE);
  233.         RECTLFROMRECT(m_rclBounds, rc);
  234.         return FALSE;
  235.         }
  236.  
  237.     //Selection logic moved to a function so OnRightDown can use it.
  238.     FSelectTenantAtPoint(x, y);
  239.  
  240.     return FALSE;
  241.     }
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. /*
  250.  * CPage::OnLeftUp
  251.  *
  252.  * Purpose:
  253.  *  Called when the user clicks up with the left button on this page.
  254.  *  We stop tracking on this message, if necessary, and resize the object.
  255.  *
  256.  * Parameters:
  257.  *  uKeys           UINT carrying the key state.
  258.  *  x, y            UINT coordinates of the click in device units.
  259.  *
  260.  * Return Value:
  261.  *  BOOL            Indicates if this action changed the object.
  262.  */
  263.  
  264. BOOL CPage::OnLeftUp(UINT uKeys, UINT x, UINT y)
  265.     {
  266.     RECT    rc, rcT;
  267.  
  268.     if (!m_fTracking)
  269.         return FALSE;
  270.  
  271.     //Remove the dotted rectangle.
  272.     RECTFROMRECTL(rc, m_rcl)
  273.     DrawFocusRect(m_hDC, &rc);
  274.     ReleaseDC(m_hWnd, m_hDC);
  275.  
  276.     ReleaseCapture();
  277.     m_fTracking=FALSE;
  278.  
  279.     //If the original and new rects are the same, nothing happened.
  280.     RECTFROMRECTL(rcT, m_rclOrg);
  281.  
  282.     if (EqualRect(&rc, &rcT))
  283.         return FALSE;
  284.  
  285.     RECTFROMRECTL(rcT, m_rclOrg);
  286.     InvalidateRect(m_hWnd, &rcT, TRUE);
  287.  
  288.     //Invalidate on the screen before accounting for scrolling
  289.     InvalidateRect(m_hWnd, &rc, TRUE);
  290.  
  291.     //Factor in the scrolling and tell the tenant where it now stands.
  292.     OffsetRect(&rc, (int)m_pPG->m_xPos, (int)m_pPG->m_yPos);
  293.     RECTLFROMRECT(m_rcl, rc);
  294.     m_pTenantCur->RectSet(&m_rcl, TRUE);
  295.  
  296.     UpdateWindow(m_hWnd);
  297.     return TRUE;
  298.     }
  299.  
  300.  
  301.  
  302.  
  303.  
  304. /*
  305.  * CPage::OnLeftDoubleClick
  306.  *
  307.  * Purpose:
  308.  *  Called when the user double-clicks with the left button on this page.
  309.  *  We find the object under that position that is visibly on top
  310.  *  (always the first one under this location in the page list since
  311.  *  we paint